/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var sortList = function (head) {
  if (!head) return head;
  let temp = head;
  let arr = [];
  while (temp) {
    arr.push(temp.val);
    temp = temp.next;
  }
  if (arr.length === 1) return new ListNode(arr[0]);
  arr.sort((a, b) => a - b);

  let myHead = new ListNode(arr[0]);
  let cur = myHead;
  for (let i = 1; i < arr.length; i++) {
    cur.next = new ListNode(arr[i]);
    cur = cur.next;
  }
  return myHead;
};
